/*
 * Copyright (c) 2011-2013, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: loginClientFunction.c 7664 2014-03-04 08:47:41Z geisinger $
 */

/**
 * \file
 *         Source file for function loginClient in component loginClient.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "xme/core/login/include-gen/loginClientFunction.h"
#include "xme/core/login/include-gen/loginClientComponentWrapper.h"

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINCLIENTFUNCTION_C_INCLUDES) ENABLED START
#include "xme/core/login/include/loginClient.h"
// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/
/**
 * \brief  Variable holding the value of the output port 'outLoginRequest'.
 *
 * \details If necessary initialize this in the init function.
 *          The value of this variable will be written to the port at the end of
 *          the step function.
 */
static xme_core_topic_login_loginRequest_t
portOutLoginRequestData;

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINCLIENTFUNCTION_C_STATICVARIABLES) ENABLED START
int count=0;
// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
xme_core_login_loginClientFunction_init
(
    void* param
)
{
    // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINCLIENTFUNCTION_INITIALIZE_C) ENABLED START
    
    XME_UNUSED_PARAMETER(param);
    xme_core_login_loginClient_init();
    xme_core_login_loginClient_fillLoginRequest(&portOutLoginRequestData);
    return XME_STATUS_SUCCESS;
    
    // PROTECTED REGION END
}

void
xme_core_login_loginClientFunction_step
(
    void* param
)
{
    xme_status_t status[1];
    
    xme_core_topic_login_loginResponse_t portInLoginResponseData;
    xme_core_topic_login_loginRequest_t* portOutLoginRequestDataPtr = &portOutLoginRequestData;
    
    status[0] = xme_core_login_loginClientComponentWrapper_readPortInResponse(&portInLoginResponseData);
    
    {
        // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINCLIENTFUNCTION_STEP_C) ENABLED START
    
        // TODO: Auto-generated stub
    
        XME_UNUSED_PARAMETER(param);
        if(XME_STATUS_SUCCESS==status[0])
                {
                    // Dont't process if non-matching guid, 
                    if (portInLoginResponseData.guid != 0) {
                        XME_LOG(XME_LOG_NOTE, "TADA");
                    }
                    if(portInLoginResponseData.guid==portOutLoginRequestData.guid)
                    {
                        if(portOutLoginRequestData.nodeId == XME_CORE_NODE_INVALID_NODE_ID)
                        {
                            xme_core_login_loginClient_processLoginResponse(&portInLoginResponseData);
                            portOutLoginRequestData.nodeId = portInLoginResponseData.nodeId;
                        }
                    }
                }
                if(portOutLoginRequestData.nodeId != XME_CORE_NODE_INVALID_NODE_ID)
                    portOutLoginRequestDataPtr = NULL;
        if(count!=0)
                    portOutLoginRequestDataPtr = NULL;
                if(count++==50)
                    count=0;
        // PROTECTED REGION END
    }
    
    status[0] = xme_core_login_loginClientComponentWrapper_writePortOutRequest(portOutLoginRequestDataPtr);
    
    {
        // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINCLIENTFUNCTION_STEP_2_C) ENABLED START
    
        // TODO: Auto-generated stub
        //       Check return values of writePort calls here
    
        // PROTECTED REGION END
    }
}

void
xme_core_login_loginClientFunction_fini(void)
{
    // PROTECTED REGION ID(XME_CORE_LOGIN_LOGINCLIENTFUNCTION_TERMINATE_C) ENABLED START
    
    // TODO: Auto-generated stub
    
    // PROTECTED REGION END
}

// PROTECTED REGION ID(XME_CORE_LOGIN_LOGINCLIENTFUNCTION_IMPLEMENTATION_C) ENABLED START

// PROTECTED REGION END
